﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCClubSite.IDAL;
using MVCClubSite.Model;

namespace MVCClubSite.BLL
{
    /// <summary>
    /// This is the business component to manage events
    /// </summary>
    public class EventsService
    {
        IEvents _events;

        /// <summary>
        /// Default constructor that will use the DAL set in the web config
        /// </summary>
        public EventsService()
        {
            _events = MVCClubSite.DALFactory.DataAccess.CreateEvents();
        }

        /// <summary>
        /// Constructor for testing with a different DAL
        /// </summary>
        /// <param name="announcements">DAL for the announcement service to use</param>
        public EventsService(IEvents events)
        {
            _events = events;
            if (_events == null)
                throw new InvalidOperationException("Events DAL cannot be null.");
        }

        /// <summary>
        /// Method to get events
        /// </summary>
        /// <returns></returns>
        public List<Event> GetEvents()
        {
            return (from e in _events.GetEvents()
                    orderby e.StartDateTime ascending
                    select e).ToList();
        }

        /// <summary>
        /// Method to get all upcoming events
        /// </summary>
        /// <returns></returns>
        public List<Event> GetUpcomingEvents()
        {
            return (from e in _events.GetUpcomingEvents()
                    orderby e.StartDateTime ascending
                    select e).ToList();
        }

        /// <summary>
        /// Method to get upcoming events
        /// </summary>
        /// <param name="days">Number of days in the future</param>
        /// <returns></returns>
        public List<Event> GetUpcomingEvents(int count)
        {
            return (from e in _events.GetUpcomingEvents()
                    orderby e.StartDateTime ascending
                    select e).Take(count).ToList();
        }

        /// <summary>
        /// Method to get all recent events
        /// </summary>
        /// <returns></returns>
        public List<Event> GetRecentEvents()
        {
            return (from e in _events.GetRecentEvents()
                    orderby e.StartDateTime descending
                    select e).ToList();
        }

        /// <summary>
        /// Method to get recent events
        /// </summary>
        /// <param name="days">Number of days in the past</param>
        /// <returns></returns>
        public List<Event> GetRecentEvents(int count)
        {
            return (from e in _events.GetRecentEvents()
                    orderby e.StartDateTime descending
                    select e).Take(count).ToList();
        }

        /// <summary>
        /// Method to get events by date
        /// </summary>
        /// <returns></returns>
        public List<Event> GetEventsByDate(DateTime date)
        {
            return (from e in _events.GetEventsByDate(date)
                    select e).ToList();
        }

        /// <summary>
        /// Method to get a events by ID
        /// </summary>
        /// <param name="id">ID of the event</param>
        /// <returns>Event business entity</returns>
        public Event GetEventByID(int id)
        {
            return _events.GetEventByID(id);
        }

        /// <summary>
        /// Method to insert or update a event
        /// </summary>
        /// <param name="location">Event to insert or update</param>
        public void Save(Event location)
        {
            if (location.CreationDate == DateTime.MinValue)
                location.CreationDate = DateTime.Now;

            _events.Save(location);
        }

        /// <summary>
        /// Method to delete a event
        /// </summary>
        /// <param name="id">ID of the event</param>
        public void Delete(int id)
        {
            _events.Delete(id);
        }
    }
}
